package com.example.androidtvdemo.demo04;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.leanback.widget.Presenter;

import com.bumptech.glide.Glide;
import com.example.androidtvdemo.R;
import com.example.androidtvdemo.net.BlogArticle;

public class BlogArticlePresenter extends Presenter {

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        View inflate = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_blog_article, parent, false);
        return new Holder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Object item) {
        Holder holder = (Holder) viewHolder;
        if (item instanceof BlogArticle.DataBean.ContentBean) {
            Log.i("->>", "onBindViewHolder: 加载数据");
            //holder.mTextView.setText((String) item);
            BlogArticle.DataBean.ContentBean data = (BlogArticle.DataBean.ContentBean) item;
            Glide.with(((Holder) viewHolder).mImageView.getContext())
                    .load("http://8.129.19.172:2020/portal/image/" +
                            ((BlogArticle.DataBean.ContentBean) item).getCover())
                    .into(holder.mImageView);
        }else {
            Log.i("->>", "onBindViewHolder: 数据不对");
        }
    }

    @Override
    public void onUnbindViewHolder(ViewHolder viewHolder) {

    }

    public static class Holder extends ViewHolder {
        private ImageView mImageView;

        public Holder(View view) {
            super(view);
            mImageView = view.findViewById(R.id.iv_article_cover);
        }
    }
}
